package com.yixun.qfbao.hander;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.yixun.qfbao.apidto.DeptTreeResDto;
import com.yixun.qfbao.apidto.LevelDeptTreeResDto;
import com.yixun.qfbao.apidto.base.CommonReqDTO;
import com.yixun.qfbao.dao.DepartmentDao;
import com.yixun.qfbao.dto.base.BaseRequestDTO;
import com.yixun.qfbao.dto.base.BaseResponseDTO;
import com.yixun.qfbao.hander.base.CommonAbstractHander;
import com.yixun.qfbao.model.Department;
import com.yixun.qfbao.model.PartnerInfo;
import com.yixun.qfbao.model.dto.DepartmentDto;
import com.yixun.qfbao.model.enums.DelFlagEnum;
import com.yixun.qfbao.utils.LoginUtils;
import com.yixun.qfbao.vo.GeneralReqVo;
import com.yixun.qfbao.xf.model.enums.ResultEnum;

/**
 * @author zilong.liu
 * @date 2020-12-16
 *
 * desc:
 *     	人员审核
 */
@Service("getLevelDeptTreeHander")
public class GetLevelDeptTreeHander extends CommonAbstractHander{

	@Autowired
	LoginUtils loginUtils;
	
	@Autowired
	DepartmentDao departmentDao;
	
	@Override
	public BaseResponseDTO doBusiness(BaseRequestDTO reqDto) {
		BaseResponseDTO<LevelDeptTreeResDto> res = new BaseResponseDTO<>();
		GeneralReqVo<CommonReqDTO> reqVo = (GeneralReqVo<CommonReqDTO>) reqDto;
		String userId = reqVo.getParams().getUserId();
		PartnerInfo partnerInfo = loginUtils.getUser(userId);
		res.setResult(ResultEnum.FAIL.getNo());
		if(partnerInfo == null) {
			res.setErrorMsg("未登录");
			return res;
		}
		
		long companyId = partnerInfo.getHigherid();
		DepartmentDto entity = new DepartmentDto();
		entity.setMerchantId(companyId);
		entity.setDelFlag(DelFlagEnum.UNDEL.getNo());
		List<Department> departmentList = departmentDao.selectAllListByDto(entity);
		if(CollectionUtils.isEmpty(departmentList)) {
			res.setErrorMsg("未查找到部门信息");
			return res;
		}
		
		List<DeptTreeResDto> deptTreeResDtoList = new ArrayList<>(departmentList.size());
		for (Department department : departmentList) {
			DeptTreeResDto deptTreeResDto = new DeptTreeResDto();
			deptTreeResDto.setValue(department.getId());
			deptTreeResDto.setParentId(department.getParentId());
			deptTreeResDto.setLabel(department.getDepartmentName());
			deptTreeResDto.setLayer(department.getLayer());
			deptTreeResDtoList.add(deptTreeResDto);
		}
		
		for (DeptTreeResDto deptTreeResDto : deptTreeResDtoList) {
			for (DeptTreeResDto deptTreeResDto2 : deptTreeResDtoList) {
				deptTreeResDto.addChildren(deptTreeResDto2);
			}
		}
		
		// 只保留0级节点
		List<DeptTreeResDto> returnDeptTreeResDtoList = new ArrayList<>();
		for (DeptTreeResDto deptTreeResDto : deptTreeResDtoList) {
			if(deptTreeResDto.getLayer() == 0) {
				returnDeptTreeResDtoList.add(deptTreeResDto);
			}
		}
		
		LevelDeptTreeResDto levelDeptTreeResDto = new LevelDeptTreeResDto();
		levelDeptTreeResDto.setDeptTreeResDtoList(returnDeptTreeResDtoList);
		res.setResData(levelDeptTreeResDto);
		res.setResult(ResultEnum.SUCCESS.getNo());
		return res;
	}

}
